package cn.xq.jltx.threecamerdemo;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2016年01月30日.
 */

public class ThreeDView extends View{
    private Matrix matrix;
    private Camera mCamera;
    private int width;
    private int height;

    private Bitmap mBitmap;
    private Bitmap mBitmap1;
    private int bmpW1, bmpH1;
    private int bmpW2, bmpH2;
    private Paint mPaint;

    private int rotateX = 0;
    private int rotateY = 0;
    private int rotateZ = 0;

    private int translateX = 0;
    private int translateY = 0;
    private int translateZ = 0;

    private int cirCentreX;
    private int cirCentreY;

    public ThreeDView(Context context) {
        super(context);
        initView();
    }

    public ThreeDView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ThreeDView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public void initView(){
        if (mCamera == null) {
            mCamera = new Camera();
        }
        if (matrix == null) {
            matrix = new Matrix();
        }

        if (mPaint == null) {
            mPaint = new Paint();
            mPaint.setStrokeWidth(5.0f);
            mPaint.setAntiAlias(true);
            mPaint.setARGB(255, 255, 0, 0);
            mPaint.setStyle(Paint.Style.STROKE);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cirCentreX = width / 2;
        cirCentreY = height / 2;
        if (mBitmap == null || mBitmap.isRecycled()) {
            mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher);
            bmpW1 = width/2;//200
            bmpH1 = height/2;
            mBitmap = Bitmap.createScaledBitmap(mBitmap, bmpW1, bmpH1, true);
        }
        if (mBitmap1 == null || mBitmap1.isRecycled()) {
            mBitmap1 = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.ic_launcher);
            bmpW2 = width/4;//50
            bmpH2 = height/4;
            mBitmap1 = Bitmap.createScaledBitmap(mBitmap1, bmpW2, bmpH2, true);
        }
    }

    public int getRotateX() {
        return rotateX;
    }

    public void setRotateX(int rotateXStep) {
        this.rotateX += rotateXStep;
        invalidate();
    }

    public int getRotateY() {
        return rotateY;
    }

    public void setRotateY(int rotateYStep) {
        this.rotateY += rotateYStep;
        invalidate();
    }

    public int getRotateZ() {
        return rotateZ;
    }

    public void setRotateZ(int rotateZStep) {
        this.rotateZ += rotateZStep;
        invalidate();
    }

    public int getTranslateX() {
        return translateX;
    }

    public void setTranslateX(int translateXStep) {
        this.translateX += translateXStep;
        invalidate();
    }

    public int getTranslateY() {
        return translateY;
    }

    public void setTranslateY(int translateYStep) {
        this.translateY += translateYStep;
        invalidate();
    }

    public int getTranslateZ() {
        return translateZ;
    }

    public void setTranslateZ(int translateZStep) {
        this.translateZ += translateZStep;
        invalidate();
    }


    public int getCirCentreX() {
        return cirCentreX;
    }

    public void setCirCentreX(int cirCentreX) {
        this.cirCentreX = cirCentreX;
        invalidate();
    }

    public int getCirCentreY() {
        return cirCentreY;
    }

    public void setCirCentreY(int cirCentreY) {
        this.cirCentreY = cirCentreY;
        invalidate();
    }

    public void reset() {
        rotateX = 0;
        rotateY = 0;
        rotateZ = 0;
        translateX = 0;
        translateY = 0;
        translateZ = 0;

        cirCentreX = width / 2;
        cirCentreY = height / 2;
        invalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.GRAY);
        int saveCount;

        //每画一次记录一下初始状态。save()和restore()可以将图像过渡得柔和一些。
        saveCount = canvas.save();
        mCamera.save();
        matrix.reset();
        //物体的3d平移，x，y，z方向
        mCamera.translate(translateX,translateY,translateZ);
        mCamera.rotateX(rotateX);
        mCamera.rotateY(rotateY);
        mCamera.rotateZ(rotateZ);
        //将Camera3位坐标转换成矩阵并存储在矩阵中
        mCamera.getMatrix(matrix);
        //得到矩阵后还原Camera为上一次的状态，为了过度平滑
        mCamera.restore();

        //图片相对于画布上的x和y的水平平移位置
//        matrix.preTranslate(cirCentreX - bmpW1 / 2,cirCentreY - bmpH1 / 2);
        //图片围绕Z轴的旋转圆点
        matrix.preTranslate(-translateX  - bmpW1 / 2,-translateY - bmpH1 / 2);
        matrix.postTranslate(translateX + bmpW1 / 2,translateY + bmpH1 / 2);
        //矩阵运用到画布
        canvas.concat(matrix);
        canvas.drawBitmap(mBitmap, 0,0, null);
        //恢复上一次记录的画布状态
        canvas.restoreToCount(saveCount);

        //每画一次记录一下初始状态。save()和restore()可以将图像过渡得柔和一些。
        saveCount = canvas.save();
        mCamera.save();
        matrix.reset();
        //物体的3d平移，x，y，z方向
        mCamera.translate(translateX,translateY,translateZ);
        mCamera.rotateX(rotateX);
        mCamera.rotateY(rotateY);
        mCamera.rotateZ(rotateZ);
        //将Camera3位坐标转换成矩阵并存储在矩阵中
        mCamera.getMatrix(matrix);
        //得到矩阵后还原Camera为上一次的状态，为了过度平滑
        mCamera.restore();

        //图片围绕Z轴的旋转圆点
        matrix.preTranslate(-translateX - bmpW2 / 2,-translateY - bmpH2 / 2);
        matrix.postTranslate(translateX + bmpW2 / 2,translateY + bmpH2 / 2);
        //矩阵运用到画布
        canvas.concat(matrix);
        canvas.drawBitmap(mBitmap1, 0,0, null);
        //恢复上一次记录的画布状态
        canvas.restoreToCount(saveCount);

        //画中心点,参考点
        canvas.drawPoint(cirCentreY, cirCentreY, mPaint);

    }
}
